<template>
  <el-dialog title="添加活动" :visible.sync="dialogFormVisible" :before-close="close" :close-on-click-modal="false">
    <el-form :model="form">
      <el-form-item label="活动名称" :label-width="formLabelWidth">
        <el-input v-model="form.activityName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="活动描述" :label-width="formLabelWidth">
      <el-input v-model="form.activityDesc" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="活动图片链接" :label-width="formLabelWidth">
        <el-input v-model="form.activityPicUrl" auto-complete="off"></el-input>
      </el-form-item>
      <upload @success="updateUrl" class="upload"></upload>
      <el-form-item label="活动链接" :label-width="formLabelWidth">
        <el-input v-model="form.activityUrl" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="活动状态" :label-width="formLabelWidth">
        <el-radio v-model="form.activityStatus" label="0">未开始</el-radio>
        <el-radio v-model="form.activityStatus" label="1">已开始</el-radio>
        <el-radio v-model="form.activityStatus" label="2">已下架</el-radio>
      </el-form-item> 
      <el-form-item label="活动排序" :label-width="formLabelWidth">
        <el-input v-model.number="form.sequence" auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import upload from 'components/upload'
import { activitySave, getactivityDetail } from 'api/activity'
export default {
  props: ['activityId'],
  components: {
    upload
  },
  data() {
    return {
      dialogFormVisible: true,
      form: {
        activityDesc: '', // 活动描述
        activityId: this.activityId || '', // 活动ID
        activityName: '', // 活动名称
        activityPicUrl: '', // 活动图片链接
        activityStatus: '1', // 活动状态
        activityUrl: '', // 活动链接
        sequence: '' // 活动排序（暂写死）
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    if (this.activityId) {
      this.getactivityDetail()
    }
  },
  methods: {
    updateUrl(parm) {
      this.form.activityPicUrl = parm
    },
    close() {
      this.$emit('close')
    },
    async submit() {
      try {
        let back = await activitySave(this.form)
        back = back.data
        if (back.resultCode === '200') {
          console.log(back.result)
          this.$emit('update')
        } else {
          this.$alert(back.msg, '提示')
        }
      } catch (err) {
        this.$alert('请求出错' + err)
      }
    },
    async getactivityDetail() {
      try {
        let back = await getactivityDetail(this.activityId)
        back = back.data
        if (back.resultCode === '200') {
          this.form.activityDesc = back.result.activityDesc
          this.form.activityName = back.result.activityName
          this.form.activityPicUrl = back.result.activityPicUrl
          this.form.activityStatus = back.result.activityStatus + ''
          this.form.activityUrl = back.result.activityUrl
          this.form.sequence = back.result.sequence
        } else {
          this.$alert(back.msg, '提示')
        }
      } catch (err) {
        this.$alert('请求出错' + err)
      }
    }
  }
}
</script>
<style scoped lang="scss">
  .upload{
    margin-left:120px;
    margin-bottom:20px;
  }
</style>
